import axios from 'axios'
import store from '@/store'
import { Message } from 'element-ui';

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})

service.interceptors.request.use(config => {
  if (store.getters.token) {
    config.headers.Authorization = `Bearer ${store.getters.token}`
  }

  return config
})
service.interceptors.response.use(res =>{
  console.log('网络层面成功',res);
  const{success,data,message} = res.data
  if(success){
    console.log('数据层面成功');
    return data
  } else{
    console.log('数据层面失败');
    Message.error(message)
    return Promise.reject(new Error(message))
  }
},err=> {
  console.log('网络层面失败',err);
  Message.error(err.message)
  return Promiae.reject(new Error(err.message))
})

export default service
